package tw.com.diray.set;

import java.util.ArrayList;
import java.util.List;

import tw.com.diary.ae.com.DirayBussinessException;
import tw.com.diary.api.domain.RecViewDTO;
import tw.com.diary.api.domain.RecViewResultDTO;
import tw.com.diary.api.service.FragmentService;
import tw.com.diary.api.service.RecViewService;
import tw.com.diary.core.RecViewServiceImpl;
import tw.com.diary.db.domain.X0001;
import tw.com.diray.DariyView;
import tw.com.diray.R;
import tw.com.diray.RecView;
import tw.com.diray.comm.ui.DirDateBean;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class RecTopFragment extends Fragment implements FragmentService {

	private RecViewDTO dto = null;

	private RecViewService service = null;

	private ClickAdapter addaptper = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.list_rec_view, container, false);
		this.addaptper = new ClickAdapter(dto.getResults());
		ListView listView = (ListView) rootView.findViewById(R.id.listview);
		listView.setAdapter(addaptper);

		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);// 多選
		listView.setMultiChoiceModeListener(new MyMultiChoiceModeLister());
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent it = new Intent(getActivity(), DariyView.class);

				Bundle bulde = new Bundle();
				bulde.putString("uuid", dto.getResults().get(position).getX001().getUuid());
				it.putExtras(bulde);

				getActivity().startActivity(it);

			}
		});

		return rootView;

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		this.dto = new RecViewDTO();
		this.service = new RecViewServiceImpl();

	}

	@Override
	public void update() throws DirayBussinessException {

		this.addaptper.notifyDataSetChanged();
	}

	@Override
	public void doQuery() throws DirayBussinessException {
		this.service.initQueryX0001s(this.dto, getActivity());

	}

	private class ClickAdapter extends BaseAdapter {

		private List<RecViewResultDTO> checks;

		private ClickAdapter(List<RecViewResultDTO> checks) {
			this.checks = checks;
		}

		public List<X0001> getSelects() {
			List<X0001> tmp = new ArrayList<X0001>();
			for (RecViewResultDTO type : checks) {
				if (type.isClicked()) {
					tmp.add(type.getX001());
				}
			}
			return tmp;
		}

		public void update() {

			this.notifyDataSetChanged();
		}

		/**
		 * 清除狀態
		 */
		public void allClean() {
			for (RecViewResultDTO type : checks) {
				type.setClicked(false);
			}
			this.notifyDataSetChanged();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			View view = getActivity().getLayoutInflater().inflate(R.layout.row_list_item, null);

			TextView title = (TextView) view.findViewById(R.id.title);
			TextView content = (TextView) view.findViewById(R.id.content);

			DirDateBean bean = new DirDateBean(checks.get(position).getX001().getYyyymmdd());

			title.setText(bean.getShowDate());
			content.setText(checks.get(position).getX001().getContent());

			final RecViewResultDTO check = this.checks.get(position);
			view.setBackgroundColor(getResources().getColor(android.R.color.transparent)); // default
																							// color
			if (check.isClicked()) {
				view.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light)); // default
																									// color
			}
			return view;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return this.checks.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return this.checks.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
	}

	/**
	 * 多選行為事件
	 * 
	 * @author Andy
	 * 
	 */
	@SuppressLint("NewApi")
	private class MyMultiChoiceModeLister implements MultiChoiceModeListener {
		private final String TAG = MyMultiChoiceModeLister.class.getName();
		int selectNo = 0;

		@Override
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {

			mode.getMenuInflater().inflate(R.menu.rec_view, menu);

			return true;
		}

		@Override
		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
			switch (item.getItemId()) {

			case R.id.action_remove:

				dto.setDeletes(addaptper.getSelects());

				service.removeX0001s(dto, RecTopFragment.this.getActivity());
				mode.finish();
				selectNo = 0;
				addaptper.allClean();
				((RecView) getActivity()).updata();
				return true;

			case R.id.action_package:
				dto.setDeletes(addaptper.getSelects());
				service.backUpX001(dto, RecTopFragment.this.getActivity());
				((RecView) getActivity()).updata();
				mode.finish();
				selectNo = 0;
				addaptper.allClean();

			default:
				mode.finish();
				addaptper.allClean();
				return true;
			}
		}

		@Override
		public void onDestroyActionMode(ActionMode mode) {
			addaptper.allClean();
			selectNo = 0;
		}

		@Override
		public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {

			RecViewResultDTO type = dto.getResults().get(position);
			if (checked) {
				selectNo++;
				type.setClicked(true);

			} else {
				selectNo--;
				type.setClicked(false);
			}

			mode.setTitle(getResources().getString(R.string.selectNo) + selectNo);
			addaptper.update();
			// TODO Auto-generated method stub

		}
	}
}
